unit uSelectUserForm;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, CheckLst, ExtCtrls, Contnrs;

type
  TSelectUserForm = class(TForm)
    clbUsers: TCheckListBox;
    Button1: TButton;
    Button2: TButton;
    radgUser: TRadioGroup;
    Button3: TButton;
    Panel1: TPanel;
    Edit1: TEdit;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure FormActivate(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure Edit1Change(Sender: TObject);
  private
    { Private declarations }
    FUserList: TObjectList;
  public
    { Public declarations }
    SelectedUsers: string;
    Candidates: string;
    FormType: Integer;
  end;

implementation

uses
  uGlobal;

{$R *.dfm}

procedure TSelectUserForm.Button1Click(Sender: TObject);
var
  i: Integer;
begin
  SelectedUsers := '';
  case FormType of
  0:
    begin
      for i := 0 to clbUsers.Items.Count - 1 do
        with TUser(clbUsers.Items.Objects[i]) do
        begin
          if clbUsers.Checked[i] then
          begin
            SelectedUsers := SelectedUsers + IntToStr(DBIdx) + ';';
          end;
        end;
    end;
  1:
    begin
      if radgUser.ItemIndex<>-1 then
        SelectedUsers := IntToStr(TUser(radgUser.Items.Objects[radgUser.ItemIndex]).DBIdx);
    end;
  end;
  ModalResult := mrOk;
end;

procedure TSelectUserForm.Button2Click(Sender: TObject);
begin
  ModalResult := mrCancel;
end;

procedure TSelectUserForm.Button3Click(Sender: TObject);
var
  i, n: Integer;
  sl: TStringList;
begin
  n := 0;
  sl := TStringList.Create;
  for i := clbUsers.Items.Count-1 downto 0 do
  begin
    if clbUsers.Checked[i] then
    begin
      Inc(n);
      sl.InsertObject(0, clbUsers.Items[i], clbUsers.Items.Objects[i]);
    end else
    begin
      sl.AddObject(clbUsers.Items[i], clbUsers.Items.Objects[i]);
    end;
  end;
  clbUsers.Items.Assign(sl);
  for i := 0 to n - 1 do
    clbUsers.Checked[i] := True;
  sl.Free;
end;

procedure TSelectUserForm.Edit1Change(Sender: TObject);
var
  i: Integer;
begin
  clbUsers.Items.Clear;
  radgUser.Items.Clear;
  for i := 0 to FUserList.Count - 1 do
  with TUser(FUserList[i]) do
  begin
    if (Trim(Edit1.Text)='') or (Pos(Trim(Edit1.Text), UserName)>0) then
    begin
      case FormType of
      0:
        clbUsers.Items.AddObject(UserName, FUserList[i]);
      1:
        radgUser.Items.AddObject(UserName, FUserList[i]);
      end;
    end;
  end;
end;

procedure TSelectUserForm.FormActivate(Sender: TObject);
var
  i: Integer;
begin
  if SameText(Candidates, '!all') then
    Myself.LoadUserList(FUserList, [utJudge])
  else
    Myself.LoadUserList(FUserList, Candidates);
  case FormType of
  0:
    begin
      for i := 0 to FUserList.Count - 1 do
        with TUser(FUserList[i]) do
        begin
          clbUsers.Items.AddObject(UserName, FUserList[i]);
          if Pos(IntToStr(DBIdx) + ';', SelectedUsers)>0 then
            clbUsers.Checked[clbUsers.Items.Count-1] := True;
        end;
      clbUsers.Visible := True;
      radgUser.Visible := False;
    end;
  1:
    begin
      for i := 0 to FUserList.Count - 1 do
        with TUser(FUserList[i]) do
        begin
          radgUser.Items.AddObject(UserName, FUserList[i]);
          if DBIdx=StrToIntDef(SelectedUsers, -1) then
            radgUser.ItemIndex := i;
        end;
      clbUsers.Visible := False;
      radgUser.Visible := True;
      Button3.Visible := False;
    end;
  end;
  Edit1.SetFocus;
end;

procedure TSelectUserForm.FormCreate(Sender: TObject);
begin
  FUserList := TObjectList.Create;
  Candidates := '!all'
end;

end.
